﻿using System;
using System.Data;
using System.Reflection;
using Dapper;
using Newtonsoft.Json;
using API_Service.DBHelper;
using System.Linq;

namespace API_Service.BizLogic.WipLogApi
{
    public class WipLogService : BaseLogicService
    {
        public WipLogService() { }
     
        public ExecutionResult AddWipLog(string viewModel)
        {
            var result = new ExecutionResult();

            dynamic vm = JsonConvert.DeserializeObject<dynamic>(viewModel);

            using (IDbConnection conn = SqlCreator.Create())
            {
                try
                {
                    string sql = @"   insert into dbo.r_wip_log_t( 
                             serial_number  ,line_name ,group_name  ,station_name ,in_station_time
                              ,error_flag ,remarks  ,create_time ,create_user )
	                          values(@serial_number,@line_name,@group_name,@station_name,@in_station_time,@error_flag
                              ,@remarks,@create_time,@create_user) ";

                    conn.Open();
                    
                    result.ExecuteCount = conn.Execute(sql, new
                    {
                        serial_number = (string)vm.serial_number,
                        line_name = (string)vm.line_name,       
                        group_name = (string)vm.group_name,
                        station_name = (string)vm.station_name,     
                        in_station_time = Convert.ToDateTime(vm.in_station_time),   
                        error_flag = (string)vm.error_flag,           
                        remarks = (string)vm.remarks,
                        create_time = DateTime.Now,         
                        create_user = (string)vm.create_user
                        //update_time = DateTime.Now,
                        //update_user = (string)vm.update_user
                    });

                    if (result.ExecuteCount > 0)
                    {
                        result.Status = true;
                        result.Message = "WipLog: " + (string)vm.serial_number + " 添加成功.";
                    }
                }
                catch (Exception ex)
                {
                    Logger.WriteError(MethodBase.GetCurrentMethod(), ex);

                    result.Status = false;
                    result.Message = "WipLog Ex: " + ex.Message;
                }
                finally
                {
                    conn.Close();
                }
            }

            return result;
        }

        public ExecutionResult<dynamic> QuerySNWIPLogInfo(string model)
        {
            var result = new ExecutionResult<dynamic>();

            var vm = JsonConvert.DeserializeObject<dynamic>(model);

            using (IDbConnection conn = SqlCreator.Create())
            {
                conn.Open();
                try
                {
                    string sql = @" SELECT serial_number
                                      , line_name
                                      , group_name
                                      , station_name
                                      , in_station_time
                                      , error_flag
                                      , remarks
                                      , create_time
                                      , create_user
                                      , update_time
                                      , update_user
                                  FROM dbo.r_wip_log_t
                                  where 1 = 1 ";
                    if (!string.IsNullOrEmpty(vm.SN.Value))
                    {
                        sql = sql + " AND serial_number = @sn ";
                    }
                    if (!string.IsNullOrEmpty(vm.GROUPNAME.Value))
                    {
                        sql = sql + " AND group_name = @gname ";
                    }

                    sql = sql + " AND in_station_time between @dtts and @dtte   ";
                    

                    result.Datas = conn.Query(sql, new { sn = vm.SN.Value, gname = vm.GROUPNAME.Value, dtts = vm.DTS.Value, dtte=vm.DTE.Value }).ToArray();
                    result.Status = true;
                    result.Message = "Query SN wip log info success.";
                }
                catch (Exception ex)
                {
                    result.Status = false;
                    result.Message = ex.Message;
                    Logger.WriteError(MethodBase.GetCurrentMethod().Name, ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return result;
        }

    }
}